package com.hxexp.weixin.mvc.resetpwd.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.constant.Errcode;
import com.hxexp.weixin.hpe.api.resetpwd.model.ResetPwdResultBean;
import com.hxexp.weixin.hpe.api.sendcaptcha.model.SendCaptchaResultBean;
import com.hxexp.weixin.mvc.resetpwd.service.ResetPwdService;

/**
 * 重置密码控制器
 *
 * @author wang.bin
 * @author li.jiang
 */
@Controller
@RequestMapping(value = "/resetpwd")
public final class ResetPwdController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResetPwdController.class);

    @Autowired
    private ResetPwdService resetPwdService;

    /**
     * 找回密码
     *
     * @param model
     * @return 找回密码视图名
     */
    @RequestMapping(value = "/index")
    public String index(Model model) {
        return "send_captcha";
    }

    /**
     * 发送重置密码验证码
     *
     * @param model
     * @return 重置密码验证码视图名
     */
    @RequestMapping(value = "/sendcaptcha")
    public String sendCaptcha(String username, Model model) {
        SendCaptchaResultBean sendCaptchaBean = null;
        String view = "";
        try {
            sendCaptchaBean = resetPwdService.sendCaptcha(username);
            if (sendCaptchaBean.getErrcode() == 0) {
                // 验证码发送成功转到重置密码视图
                view = "reset_pwd";
                model.addAttribute("username", username);
                model.addAttribute("mobile", sendCaptchaBean.getMobile());
                model.addAttribute("SUCCESS", Errcode.SUCCESS);
                model.addAttribute("CAPTCHA_INVALID", Errcode.CAPTCHA_INVALID);
            } else {
                // 验证码发送失败转到错误提示视图
                view = "send_captcha_tips"; // NOSONAR
                model.addAttribute("msg", sendCaptchaBean.getErrmsg());
            }
        } catch (ServiceException e) {
            LOGGER.error("调用发送密码验证码时发生异常", e);
            view = "send_captcha_tips"; // NOSONAR
            model.addAttribute("msg", "服务器内部错误");
        }

        return view;
    }

    /**
     * 重置密码
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/resetpassword")
    @ResponseBody
    public ResetPwdResultBean resetPassword(String captcha, String username, Model model) {
        ResetPwdResultBean resetpwd = null;
        try {
            resetpwd = resetPwdService.resetPwd(captcha, username);
        } catch (ServiceException e) {
            LOGGER.error("调用重置密码时发生异常", e);
            resetpwd = new ResetPwdResultBean();
            resetpwd.setErrcode(Errcode.SERVER_INTERNAL_ERROR);
            resetpwd.setErrmsg("服务器内部错误 ");
        }
        return resetpwd;
    }
}
